<script setup lang="ts">
import {onBeforeUnmount, onMounted, ref} from "vue";
import {getOrderPositionAll,getOrderHistoryList} from "@/api/orderApi.ts";

//获取持仓数据
const positionOrders = ref([]);
const fetchPositionRecordData = () => {
  getOrderPositionAll({}).then(res => {
    positionOrders.value = res.data
  })
}
const intervalId = ref(0);
const startUpdating = () => {
  intervalId.value = setInterval(() => {
    fetchPositionRecordData();
  }, 500);
}

//获取历史数据
const historyOrders = ref([]);
const historyOrderFilter = ref({
  page: 1
});
const historyOrderLoading = ref(false);
const historyOrderFinished = ref(false);
const fetchHistoryRecordData = () => {
  historyOrderLoading.value = true;

  getOrderHistoryList(historyOrderFilter.value).then(res => {
    if (res.data.length > 0) {
      historyOrders.value.push(...res.data);
      historyOrderFilter.value.page += 1;
    } else {
      historyOrderFinished.value = true;
    }
    historyOrderLoading.value = false;
  })
}

const status = ref(0);

const handleNav = (params: any) => {
  if(params == 1){
    clearInterval(intervalId.value);
  } else {
    startUpdating();
  }
  status.value = params;
}


onMounted(() => {
  fetchPositionRecordData();
  startUpdating();
});

// 在组件卸载之前清除定时器
onBeforeUnmount(() => {
  clearInterval(intervalId.value);
});
</script>

<template>
  <div class="order">
    <div class="order-nav">
      <span :class="[{ 'order-nav-action': status == 0 }]" @click="handleNav(0)">{{
          $t('order.positionDetails')
        }}</span>
      <span :class="[{ 'order-nav-action': status == 1 }]" @click="handleNav(1)">{{
          $t('order.historicalDetails')
        }}</span>
    </div>
    <div v-if="status == 0">
        <div v-for="item in positionOrders" class="position-item">
          <div>
            <span style="font-size: 20px;">{{ item.product_name }}</span>
            <span>{{ item.buyprice }}-{{ item.new_price }}</span>
            <span style="color:#323232;">{{ item.buytime }}</span>
          </div>
          <div>
            <span v-if="item.type==1" style="color: #fa2e42">{{ $t('order.maizhang') }}({{ $t('currency.symbol') }}{{ item.fee }})</span>
            <span v-if="item.type==2" style="color: #1fc65b">{{ $t('order.maidie') }}({{ $t('currency.symbol') }}{{ item.fee }})</span>
          </div>
          <div>
            <span style="font-size: 20px;">{{ item.time }}</span>
          </div>
        </div>
    </div>
    <div v-if="status == 1">
      <van-list
          v-model:loading="historyOrderLoading"
          :finished="historyOrderFinished"
          :finished-text="$t('order.empty')"
          @load="fetchHistoryRecordData"
      >
      <div v-for="item in historyOrders" class="history-item">
        <div>
          <span style="font-size: 20px;">{{ item.product_name }}</span>
          <span>{{ $t('order.time') }}{{ item.endprofit }}{{ $t('order.second') }}</span>
          <span>{{ item.buytime }}</span>
        </div>
        <div>
          <span v-if="item.type == 1" style="color: #fa2e42">{{ $t('order.maizhang') }}({{ $t('currency.symbol') }}{{ item.fee }})</span>
          <span v-if="item.type == 2" style="color: #1fc65b">{{ $t('order.maidie') }}({{ $t('currency.symbol') }}{{ item.fee }})</span>
        </div>
        <div style="font-size: 20px;">
          <span v-if="item.ploss > 0" style="color: #fa2e42">{{ $t('currency.symbol') }}{{ item.ploss }}</span>
          <span v-else style="color: #1fc65b">{{ $t('currency.symbol') }}{{ item.ploss }}</span>
        </div>
      </div>
      </van-list>
    </div>
  </div>
</template>

<style scoped lang="less">
.order {
  background-color: #ffffff;

  .order-nav {
    display: flex;
    gap: 20px;
    font-size: 18px;
    margin-bottom: 10px;
    padding: 10px;
  }

  .order-nav-action {
    font-weight: bold;
  }

  .position-item {
    padding: 5px 10px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #333333;
    font-size: 12px;

    div {
      display: flex;
      flex-direction: column;
      gap: 5px;
    }
  }

  .history-item {
    padding: 5px 10px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #333333;
    font-size: 12px;

    div {
      display: flex;
      flex-direction: column;
      gap: 5px;
    }
  }
}
</style>